import * as sqlite_opt from "@/utils/sqliteUtil.js";

const MUST_TABLES_INFOS = [{
	name: "t_glossary",
	createSqlStr: `"id" INTEGER PRIMARY KEY AUTOINCREMENT,"cls" INT ,"txt_value" TEXT, "ordr" INT  `,
}]



export function initSqlite() {
	return new Promise((resolve, reject) => {

		let createTablesSuccess = (e) => {
			console.log("createTablesSuccess")
			console.log(e)
		}

		let createAllTablesFail = (e) => {
			console.log("createAllTablesFail")
			console.log(e)
		}

		let getAllTablesSuccess = (e) => {
			console.log("getAllTablesSuccess")
			console.log(e)
		}

		let getAllTablesFail = (e) => {
			console.log("getAllTablesFail")
			console.log(e)
		}

		let openDBSuccess = (e) => {
			console.log("openDBSuccess")
			console.log(e);

			// MUST_TABLES_INFOS.forEach(e => {
			// 	// let getAllTablesSql = `SELECT * FROM sqlite_master where type='table' order by name`;
			// 	let getAllTablesSql = `create table if not exists ${e.name}(${e.createSqlStr})`;

			// 	sqlite_opt.selectSql(getAllTablesSql, createTablesSuccess, createAllTablesFail);
			// })

			let getAllTablesSql = `SELECT name FROM sqlite_master where type='table' order by name`;
			sqlite_opt.selectSql(getAllTablesSql, getAllTablesSuccess, getAllTablesFail);
		}

		let openDBFail = (e) => {
			console.log("openDBFail")
			console.log(e)
		}


		console.log("initSqlite === 0")
		if (!sqlite_opt.isDbOpen())
			sqlite_opt.openDB(openDBSuccess, openDBFail);
		else {
			openDBSuccess();
		}

		console.log("initSqlite === 2")

		resolve({
			message: "ok"
		});
	})
}

export function genLocalDrawPath(inDrwPass, inPltPath) {
	return new Promise((resolve, reject) => {
		let localTemp = '_www/static/draws/' + inPltPath + inDrwPass;
		console.log("genLocalDrawPath  localTemp -----" + localTemp);
		plus.io.resolveLocalFileSystemURL(localTemp, 
			(entry) => {
				let temp = entry.toRemoteURL();
				console.log(temp);
				resolve(temp);
			},
			(e)=> {
				console.log("genLocalDrawPath  fail ---  = ");
				console.log(e);
				reject(e);
			});
	})
}